import request from './axios'

export function createPurchaseRequest(data, attachment) {
  const formData = new FormData()
  formData.append('purchaseRequest', new Blob([JSON.stringify(data)], { type: 'application/json' }))
  if (attachment) {
    formData.append('attachment', attachment)
  }

  return request({
    url: '/purchase-requests',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function getPurchaseRequests(params) {
  return request({
    url: '/purchase-requests',
    method: 'get',
    params
  })
}

export function getPurchaseRequestById(id) {
  return request({
    url: `/purchase-requests/${id}`,
    method: 'get'
  })
}

export function updatePurchaseRequest(id, data) {
  return request({
    url: `/purchase-requests/${id}`,
    method: 'put',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function deletePurchaseRequest(id) {
  return request({
    url: `/purchase-requests/${id}`,
    method: 'delete'
  })
}

export function approvePurchaseRequest(id, data) {
  return request({
    url: `/purchase-requests/${id}/approve`,
    method: 'post',
    data
  })
}

export function getPendingPurchaseRequests(params) {
  return request({
    url: '/purchase-requests/pending',
    method: 'get',
    params
  })
}

export function getAllPurchaseRequests(params) {
  return request({
    url: '/purchase-requests/all',
    method: 'get',
    params: {
      ...params,
      page: params.page || 0,
      size: params.size || 10
    }
  })
}